<template>
  <div class="app-container">
    <!-- Filter -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="数据库操作名称" style="width: 200px;" class="filter-item" />
      <el-button type="primary" class="filter-item" @click="handleFilter">查询</el-button>
      <el-button type="primary" style="margin-left: 20px" class="filter-item" @click="handleCreate">新增</el-button>
    </div>
    <!-- Table -->
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column label="ID" prop="id">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据库操作名称" prop="name">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据库" prop="data_base">
        <template slot-scope="scope">
          <span>{{ scope.row.data_base }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="add_time">
        <template slot-scope="scope">
          <span>{{ changeDate(scope.row.add_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" prop="edit_time">
        <template slot-scope="scope">
          <span>{{ changeDate(scope.row.edit_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="warning" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" />
    <!-- create or edit dialog-->
    <el-dialog :visible.sync="dialogFormVisible" :title.sync="textMap[dialogStatus]">
      <el-form :model="databaseForm" :inline="true">
        <el-form-item label="名称">
          <el-input v-model="databaseForm.name" type="text" maxlength="30" show-word-limit />
        </el-form-item>
        <el-form-item label="数据库">
          <el-select v-model="databaseForm.data_base">
            <el-option v-for="item in dbList" :key="item.index" :label="item.value" :value="item.index" />
          </el-select>
        </el-form-item>
      </el-form>
      <!-- sql脚本参数列表-->
      <el-card class="card">
        <div slot="header">
          <p>参数</p>
        </div>
        <el-button class="addButton" type="primary" icon="el-icon-plus" size="small" />
        <el-table :data="paramsList" border>
          <el-table-column label="参数名" prop="name">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" />
            </template>
          </el-table-column>
          <el-table-column label="参数说明" prop="description">
            <template slot-scope="scope">
              <el-input v-model="scope.row.description" />
            </template>
          </el-table-column>
          <el-table-column label="参数值" prop="value">
            <template slot-scope="scope">
              <el-input v-model="scope.row.value" />
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- sql脚本 -->
      <el-card class="card">
        <div slot="header">
          <p>sql</p>
        </div>
        <el-input v-model="databaseForm.sql" type="text" maxlength="500" show-word-limit></el-input>
      </el-card>
      <!-- 返回参数列表 -->
      <el-card class="card">
        <div slot="header">
          <p>返回参数</p>
        </div>
        <el-button class="addButton" type="primary" icon="el-icon-edit" size="small" />
        <el-table :data="returnList" border>
          <el-table-column label="参数名" prop="name">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" />
            </template>
          </el-table-column>
          <el-table-column label="参数说明" prop="description">
            <template slot-scope="scope">
              <el-input v-model="scope.row.description" />
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible=false">取消</el-button>
        <el-button size="small" type="primary" @click="dialogStatus==='create'?createData():updateData()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDataBaseList, deleteDataBase } from '@/api/dataBase'
import Pagination from '@/components/Pagination/index'
import { dateFormat } from '@/utils/data'

export default {
  name: 'List',
  components: { Pagination },
  data() {
    return {
      list: [],
      projectList: [],
      total: 0,
      loading: false,
      listQuery: {
        name: '',
        page: 1,
        limit: 20
      },
      dialogFormVisible: false,
      databaseForm: {
        id: undefined,
        name: '',
        data_base: '192.168.1.1',
        params: '',
        sql: '',
        returnParams: ''
      },
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      dbList: [
        {
          index: 'testDb',
          value: '192.168.1.1'
        }
      ],
      paramsList: [],
      returnList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    changeDate: function(attr) {
      return dateFormat(attr)
    },
    handleFilter() {
      this.getList()
    },
    getList() {
      this.loading = true
      getDataBaseList(this.listQuery).then((response) => {
        this.list = response.results
        this.total = response.count
        this.loading = false
      })
    },
    resetForm() {
      this.databaseForm = {
        id: undefined,
        name: '',
        dbIp: '192.168.1.1',
        params: '',
        sql: '',
        returnParams: ''
      }
    },
    handleCreate() {
      this.$router.push({ name: 'dataBaseCreate' })
    },
    handleEdit(id) {
      this.$router.push({ name: 'dataBaseEdit', params: { id: id }})
    },
    handleDelete(id) {
      deleteDataBase(id).then(() => {
        this.getList()
      })
    }
  }
}
</script>

<style scoped>
.card {
  margin-top: 15px;
}
.addButton {
  margin-bottom: 10px;
}
</style>
